﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using SAICAPPWEB.Models;
using SAICAPPWEB.BLL;
using Newtonsoft.Json;
using SAICAPPWEB.Service;
using Newtonsoft.Json.Linq;

namespace SAICAPPWEB.Controllers
{
    public class AppUserController : Controller
    {
        /// <summary>
        /// 第一次登录，写入手机设备号码
        /// </summary>
        /// <param name="appuser">Appuser.</param>
        public ActionResult Token(AppUser appuser)
        {
            Message msg = new Message();
            try
            {

                var userbll = Factory<AppUserBll>.GetService();
                appuser.PhoneSn = appuser.PhoneSn.Replace("-", "");
                var user = userbll.GetUserByPhoneNo(appuser);
                if (user != null)
                {

                    if (string.IsNullOrWhiteSpace(user.PhoneSn))
                    {
                        user.PhoneSn = appuser.PhoneSn;
                        userbll.Update(user);
                        user = userbll.GetUserByPhoneNo(appuser);
                        msg.data = user;
                    }
                    else
                    {
                        if (user.PhoneSn != appuser.PhoneSn)
                        {
                            msg.error = true;
                            msg.message = "您的电话号码已经跟另外一台手机绑定，请联系管理员解绑后重新登录！";
                        }
                        else
                        {
                            msg.data = user;
                        }
                    }
                }
                else {
                    msg.error = true;
                    msg.message = "请联系管理员，没有跟您号码匹配的账号！";

                }
            }
            catch (Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);
        }

        /// <summary>
        /// 根据手机号和设备号进行登录
        /// </summary>
        /// <param name="appuser">Appuser.</param>
        public ActionResult LoginByToken(AppUser appuser)
        {
            Message msg = new Message();
            try
            {
                appuser.PhoneSn = appuser.PhoneSn.Replace("-", "");
                var userbll = Factory<AppUserBll>.GetService();
                var user = userbll.GetUserByToken(appuser);

                if (user != null)
                {
                    msg.data = 1;
                }
                else {
                    msg.data = 0;
                }
            }
            catch (Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);
        }
        /// <summary>
        /// 根据手机号和设备号签到
        /// </summary>
        /// <param name="appuser">Appuser.</param>
        public ActionResult SignIn(TimeSheet timesheet)
        {
            timesheet.PhoneSn = timesheet.PhoneSn.Replace("-", "");
            var timesheetbll = Factory<TimeSheetBll>.GetService();
            return Json(timesheetbll.SignIn(timesheet));
        }
        /// <summary>
        /// 根据签出
        /// </summary>
        /// <param name="appuser">Appuser.</param>
        [HttpPost]
        public ActionResult SignOut()
        {
            Message msg = new Message();
            try
            {
                Stream stream = System.Web.HttpContext.Current.Request.InputStream;
                byte[] bt = new byte[stream.Length];
                stream.Read(bt, 0, (int)stream.Length);
                string s = Encoding.UTF8.GetString(bt);
                JObject jo = JObject.Parse(s);
                //获取得到二维数组
                string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
                string[] names = jo.Properties().Select(item => item.Name.ToString()).ToArray();
                var projecttimebll = Factory<ProjectTimeBll>.GetService();
                //获取收集
                string phonesn = values[values.Length - 1].Replace("-", "");

                bool find = projecttimebll.Exit(phonesn);
                if (!find)
                {
                    var tsbll = Factory<TimeSheetBll>.GetService();
                    var signout = tsbll.SignOut(new TimeSheet() { PhoneSn = phonesn });//判断是否已经签到
                    if (signout.error)
                    {
                        return Json(signout);
                    }
                    for (int i = 0; i < names.Length - 1; i = i + 2)
                    {
                        string[] times = names[i].Split('-');
                        string projectname = times[0];
                        int projectid;
                        int.TryParse(times[1], out projectid);
                        ProjectTime pt = new ProjectTime();
                        pt.Date = DateTime.Now.ToString("yyyyMMdd");
                        double worktime = 0;
                        double.TryParse(values[i], out worktime);
                        pt.WorkTime = worktime;
                        string content = values[i + 1];
                        pt.Content = content;
                        pt.PhoneSn = phonesn;
                        pt.ProjectID = projectid;
                        pt.ProjectName = projectname;


                        projecttimebll.Insert(pt);
                    }
                    msg.data = 1;
                }
                else
                {
                    msg.error = true;
                    msg.message = "您今天已经提交，本次提交无效！";
                }

            }
            catch (Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);
        }

        /// <summary>
        /// 根据签出
        /// </summary>
        /// <param name="appuser">Appuser.</param>
        [HttpPost]
        public ActionResult GetProjects()
        {
            Message msg = new Message();
            try
            {
                var projectbll = Factory<ProjectBll>.GetService();
                var project = projectbll.GetAll();
                msg.data = project;

            }
            catch (Exception ex)
            {
                msg.error = true;
                msg.message = ex.Message;
            }
            return Json(msg);


        }
        public bool getAdmin()
        {
            return true;
        }
    }
}